package monsters
{
	/**
	 * ...
	 * @author ...
	 */
	import org.flixel.*;
	import states.PlayState;
	 
	public class Upper extends FlxSprite
	{
		[Embed(source = '../../assets/boquinha.png')] private var boquinhaPNG:Class;
		[Embed(source = '../../assets/niglet.png')] private var nigletPNG:Class;
		
		public var isDying:Boolean = false;
		
		private var aceleracao:Number = -0.5;
		private var yOriginal:int;
		
		private var timer : FlxTimer;
		private var time : Number = 5;
		private var mexer:Boolean = false;
		
		public function Upper(x:int, y:int)
		{
			super(x * 32, y * 32);
			
			yOriginal = y * 32;
			
			if (PlayState.levelInt == 0)
			{
				loadGraphic(boquinhaPNG, true, true, 32, 32);
				addAnimation("upNormal", [0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1], 20, true);
				addAnimation("upGelo", [7, 8, 9, 10, 11, 12, 13, 12, 11, 10, 9, 8], 15, true);
				addAnimation("upFogo", [14, 15, 16, 17, 18, 19, 20, 19, 18, 17, 16, 15], 30, true);
				play("upNormal");
				
			}else if(PlayState.levelInt == 1)
			{
				loadGraphic(nigletPNG, true, true, 32, 32);
				addAnimation("upNormal", [0, 1, 2, 3, 4, 3, 2, 1], 10, true);
				addAnimation("upGelo", [5, 6, 7, 8, 9, 8, 7, 6], 10, true);
				addAnimation("upFogo", [10, 11, 12, 13, 14, 13, 12, 11], 10, true);
				play("upNormal");
				
			}else if (PlayState.levelInt == 2)
			{
				loadGraphic(boquinhaPNG, true, true, 32, 32);
				addAnimation("upNormal", [0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1], 20, true);
				addAnimation("upGelo", [7, 8, 9, 10, 11, 12, 13, 12, 11, 10, 9, 8], 15, true);
				addAnimation("upFogo", [14, 15, 16, 17, 18, 19, 20, 19, 18, 17, 16, 15], 30, true);
				play("upNormal");
			}
			
			facing = FlxObject.RIGHT;
			
			timer = new FlxTimer();
			timer.start(time, 1, move);
		}
		public function change(controle:int):void
		{			
			if (controle <= 0) {
				init();		
				
			}else if (controle > 0 && controle <= 1) {
				initFire();
				
			}else if (controle > 1 && controle <= 2) {
				initGelo();
			}
		}
		
		private function init():void
		{	
			play("upNormal");
			if (this.aceleracao > 0)
			{
				this.aceleracao = 1;
				
			}else 
			{
				this.aceleracao = -1;
			}
			
			this.time = 3;
		}
		
		private function initFire():void
		{			
			play("upGelo");
			if (this.aceleracao > 0)
			{
				this.aceleracao = 0.5;
				
			}else 
			{
				this.aceleracao = -0.5;
			}
			this.time = 5;
		}
		
		private function initGelo():void
		{
			play("upFogo");
			if (this.aceleracao > 0)
			{
				this.aceleracao = 1.3;
				
			}else 
			{
				this.aceleracao = -1.3;
			}
			this.time = 1;
		}
		
		override public function update():void
		{
			super.update();
			
			if (y > yOriginal || y < (yOriginal - 32))
			{
				aceleracao *= -1;
				
			}
			
			if (mexer) {
				y += aceleracao;
				if (y == yOriginal)
				{
					mexer = false;
					timer.start(time, 1, move);
				}
			}
			
		}
		
		private function move(timer : FlxTimer):void
		{
			mexer = true;
		}
	}
}